package com.xinchuang.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinchuang.comment.R;
import com.xinchuang.entity.repacigi.RepacigiBaseTable;
import com.xinchuang.mapper.repacigi.RepacigiBaseTableMapper;
import com.xinchuang.service.MbManageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

@Service
public class MbManageServiceImpl implements MbManageService {
    @Resource
    private RepacigiBaseTableMapper repacigiBaseTableMapper;
    private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

    @Override
    public String mbList(JSONObject jsonObject) {
        //jsonobject解析获取页码
        String pageNum = jsonObject.getString( "pageNum" );
        //jsonobject解析获取页容
        String pageSize = jsonObject.getString( "pageSize" );
        //jsonobject解析获取关键字
        String keyword = jsonObject.getString( "keyword" );
        //jsonobject解析获取国家列表
        JSONArray country = jsonObject.getJSONArray( "country" );
        //jsonobject解析获取分类列表
        JSONArray classify = jsonObject.getJSONArray( "classify" );
        if (
                "{}".equals( JSON.toJSONString( country ) )
                        &&
                        "{}".equals( JSON.toJSONString( classify ) )
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String subList(JSONObject jsonObject) {
       return "";
    }

    @Override
    public String mbInfo(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String subInfo(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        //jsonobject解析获取子MB标识
        String subId = jsonObject.getString( "subId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String mbDetail(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String subDetail(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        //jsonobject解析获取子MB标识
        String subId = jsonObject.getString( "subId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String mbInfoEdit(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String subInfoEdit(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        //jsonobject解析获取子MB标识
        String subId = jsonObject.getString( "subId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String mbDetailEdit(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String subDetailEdit(JSONObject jsonObject) {
        //jsonobject解析获取子MB标识
        String subId = jsonObject.getString( "subId" );
        //jsonobject解析获取MB标识
        String tarId = jsonObject.getString( "tarId" );
        if (jsonObject == null || jsonObject.size() < 1
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String mbDelete(JSONObject jsonObject) {
        //jsonobject解析获取MB标识
        JSONArray tarIds = jsonObject.getJSONArray( "tarIds" );
        if (
                "{}".equals( JSON.toJSONString( tarIds ) )
        ) {
            return "";
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

    @Override
    public String subDelete(JSONObject jsonObject) {
        //MB标识
        String tarId = jsonObject.getString( "tarId" );
        //MB标识
        JSONArray subIds = jsonObject.getJSONArray( "subIds" );
        if (ObjectUtil.isNotEmpty( subIds )){
            repacigiBaseTableMapper.delete( new LambdaQueryWrapper<RepacigiBaseTable>()
            .eq( RepacigiBaseTable::getType,"1" )
            .eq( StrUtil.isNotBlank( tarId ), RepacigiBaseTable::getParentId,tarId )
            .in( RepacigiBaseTable::getTypeId,subIds )
            );
            return JSON.toJSONString( R.success( "删除成功" ) );
        }
        return JSON.toJSONString( R.success( "无数据" ) );
    }

}
